package cn.sharp.bbs.controller.security;

import cn.sharp.bbs.common.Result;
import cn.sharp.bbs.entity.BO.LoginEntity;
import cn.sharp.bbs.service.LoginService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Resource
    private LoginService loginService;

    /**
     * 用户登陆
     * 成功后会同时生成token缓存, 并发送回前端
     * @param entity 登陆实体
     * @return 成功返回token, 失败返回错误
     */
    @PostMapping("")
    public Result login(@RequestBody LoginEntity entity) {

        if(loginService.checkLogin(entity)) {
            return Result.success((Object) loginService.doLogin(entity));
        }
        return Result.fail(Result.ERR_CODE_BUSINESS, "账号或密码错误！");
    }
}
